﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
using System.Security.Cryptography;

namespace BUS
{
    public class TaiKhoanBUS
    {
        public string getMD5(string pass)
        {
            string str = "";
            Byte[] buffer = System.Text.Encoding.UTF8.GetBytes(pass);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            buffer = md5.ComputeHash(buffer);
            foreach (Byte b in buffer)
            {
                str += b.ToString("x2");
            }
            return str;
        }


        public TaiKhoanDTO layThongTinTaiKhoan(string username, string password)
        {
            //ma hoa password
            TaiKhoanDAO dao = new TaiKhoanDAO();
            try
            {
                return dao.layThongTinTaiKhoan(username, this.getMD5(password));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public TaiKhoanDTO layThongTinTaiKhoan(string username)
        {
            //ma hoa password
            TaiKhoanDAO dao = new TaiKhoanDAO();
            try
            {
                return dao.layThongTinTaiKhoan(username);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Thuc hien dang ky tai khoan moi voi trang thai la 0 - nghia la pending
        /// </summary>
        /// <param name="tk"></param>
        /// <param name="kh"></param>
        /// <returns></returns>
        public bool dangKyTaiKhoanMoi(TaiKhoanDTO tk, KhachHangDTO kh)
        {
            TaiKhoanDAO tkDao = new TaiKhoanDAO();
            try
            {
                tk.Password = getMD5(tk.Password);                
                string res = tkDao.dangKyTaiKhoanMoi(tk, kh);
                if (tk == null)
                    return false;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Kich hoat tai khoan set trang thai tai khoan thanh 1
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool kichHoatTaiKhoan(string username)
        {
            TaiKhoanDAO dao = new TaiKhoanDAO();
            try
            {
                return dao.kichHoatTaiKhoan(username);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
